package jwdt.xadmin.rskq.action;

import java.io.File;

import javax.servlet.ServletContext;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.rskq.bean.TRsxx;
import jwdt.xadmin.rskq.service.RsxxManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.ServletActionContext;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;
import com.ezerg.web.fileupload.FileItem;

@Controller("/xadmin/rskq")
public class RsxxManageAction extends BaseAction {
	private RsxxManageService rsxxManageService = (RsxxManageService)app.getBean("rsxxManageService");
	protected String url = "/xadmin/rskq/";
	
	@RequestMapping("/KqcxManage")
	public Page doKqcxManage(Form f, Module m) throws Exception {
		return m.findPage("xadmin/KqcxManage");
	}
	@RequestMapping("/rsxxlistManage")
	public Page doRsxxManage(Form f, Module m) throws Exception {
		//
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		TRsxx tc = (TRsxx)f.copy2Class(TRsxx.class);
		return m.getWritePage(f,rsxxManageService.doList(pu,tc).toJson());	
	}
	
	@RequestMapping("/rsxxAdd")
	public Page doAdd(Form f, Module m) throws Exception {
		//
		f.add("rsxx_sex", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=8"));
		f.add("rsxx_nation", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=20"));
		f.add("rsxx_origo", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=19"));
		f.add("rsxx_depid", Jdb.get().query("SELECT id, name FROM t_dep "));
		f.add("rsxx_depid2", Jdb.get().query("SELECT id, name FROM t_dep "));
		f.add("rsxx_xxstatus", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=21"));
		return m.findPage(url + "rsxxAdd");
	}
	
	@RequestMapping("/rsxxAddact")
	public Page doAddact(Form f, Module m) throws Exception {
		//
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + "uploadfile/";
		File up = new File(uploadPath);
		try {
			if (!up.exists()) {
				up.mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//
		TRsxx obj = (TRsxx) f.copy2Class(TRsxx.class);
		
		try {
			FileItem file = f.getFileItem("facepic");
			if (file != null) {
				String fileExt = file.getFileExt();
				String fileName = DateUtil.getComplexDateTime() + "." + fileExt;
				obj.setFacepic(fileName);
				String absolutePath = uploadPath + fileName;
				file.write(absolutePath);
			}
			obj.setEditdate(DateUtil.getDateTime());
			Jdb.get().add(obj);
		} catch (Exception e) {
			
			e.printStackTrace();
		}

		return m.getWritePage(f, rsxxManageService.doAddact(obj));
	}
	
	@RequestMapping("/rsxxEdit")
	public Page doEdit(Form f, Module m) throws Exception {
		//
		
		int id = f.getInt("id");
		TRsxx obj = (TRsxx) Jdb.get().get(TRsxx.class, id);
		if (obj == null) {
			f.add("error", "您要修改的记录不存在：id=" + id);
			return m.findPage("error");
		}
		f.add("obj", obj);
		// obj.setBirth(obj.getBirth()+" "+DateUtil.getBirth().substring(11));
		f.add("rsxx_sex", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=8"));
		f.add("rsxx_nation", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=20"));
		f.add("rsxx_origo", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=19"));
		f.add("rsxx_depid", Jdb.get().query("SELECT id, name FROM t_dep "));
		f.add("rsxx_depid2", Jdb.get().query("SELECT id, name FROM t_dep "));
		f.add("rsxx_xxstatus", Jdb.get().query("SELECT MXMC, MXDM FROM jwdt_zd_flmx WHERE flid=21"));
		return m.findPage(url + "rsxxEdit");
	}
	
	@RequestMapping("/rsxxEditact")
	public Page doEditact(Form f, Module m) throws Exception {
		//
		int id = f.getInt("id");
		TRsxx obj = (TRsxx) f.copy2Class(TRsxx.class);
		return m.getWritePage(f, rsxxManageService.doEditact(obj,id));
	}
	
	@RequestMapping("/rsxxDel")
	public Page doDel(Form f, Module m) throws Exception {
		//
		int id = f.getInt("id");
		return m.getWritePage(f, rsxxManageService.doDelact(id));
	}

}